package ywpt.service.config;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.AssetsInfo;
import ywpt.model.CommonTable;
import ywpt.model.Department;
import ywpt.model.PeopleInfo;
import ywpt.web.common.PublicFunction;

public class AssetsInfoServiceImpl extends HibernateEntityDao<AssetsInfo> implements AssetsInfoService{

	@Override
	public String creatAssetsNo() {
		DetachedCriteria query = DetachedCriteria.forClass(AssetsInfo.class);
		String assetsNo="";
		String code="";
		Integer maxId=new Integer(0);
		query.add(Restrictions.eq("deleteFlag", false));
		query.addOrder(Order.desc("id"));
		List<AssetsInfo> assetsInfo = this.getAssetsInfos(query);
		if ( assetsInfo.iterator().hasNext() )	
		{
			code = assetsInfo.iterator().next().getAssetsNo();
			maxId = new Integer(code);
		}
		assetsNo=PublicFunction.getNextCode("", maxId, false,5); 
		return assetsNo;
	}

	@Override
	public AssetsInfo getAssetsInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getAssetsInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AssetsInfo> getAssetsInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public void removeAssetsInfo(Long id) {
		AssetsInfo assetsInfo=this.get(id);
		assetsInfo.setDeleteFlag(true);
		super.update(assetsInfo);
	}

	@Override
	public void saveAssetsInfo(AssetsInfo assetsInfo, Long typeId,
			Long departId, Long peopleId) {
		if(typeId!=null)
			assetsInfo.setAssetsType(this.get(CommonTable.class, typeId));
		if(departId!=null)
			assetsInfo.setManageOrg(this.get(Department.class, departId));
		if(peopleId!=null)
			assetsInfo.setManageUser(this.get(PeopleInfo.class, peopleId));
		super.save(assetsInfo);
	}

	@Override
	public void updateAssetsInfo(AssetsInfo assetsInfo, Long typeId,
			Long departId, Long peopleId) {
		if(typeId!=null)
			assetsInfo.setAssetsType(this.get(CommonTable.class, typeId));
		if(departId!=null)
			assetsInfo.setManageOrg(this.get(Department.class, departId));
		if(peopleId!=null)
			assetsInfo.setManageUser(this.get(PeopleInfo.class, peopleId));
		super.update(assetsInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AssetsInfo> getAssetsInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

}
